<?php

namespace App\Http\Services;

use App\Http\Helpers\Mail;

class MailService{

    /**
     *邮件发送
     * @author lican<364548233@qq.com>
     */
    public static function sendEmail($email){
        $res = self::checkEmail($email);
        if(!$res){
            return MsgService::getMsg(100053);
        }
        $code = str_pad(random_int(1, 9999), 4, 0, STR_PAD_LEFT);// 生成4位随机数，左侧补0

        return Mail::send($email, $code);
    }

    /**
     *邮件格式检查
     * @author lican<364548233@qq.com>
     */
    public static function checkEmail($email){
        $reg= "/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/";
        return preg_match($reg,$email);
    }

    /**
     *验证邮箱验证码
     * @author: lican<364548233@qq.com>
     */
    public static function checkCode($email, $code){
        if(empty($code) || strlen($code) != 4){
            return MsgService::getMsg(100052);
        }
        $verify = app('redis')->get($email);
        if(empty($verify)){
            return MsgService::getMsg(100051);
        }
        if($code !== $verify){
            return MsgService::getMsg(100052);
        }
        return MsgService::getMsg(0);
    }

}
